#include<iostream>
using namespace std;
struct TreeNode {
    TreeNode* Leftchild;
    int value;
    TreeNode* Rightchild;
};


TreeNode* creat(int n) {
    TreeNode* Store[30] = { NULL };
    TreeNode* temp;
    for (int i = 0; i < n; i++) {
        temp = new TreeNode;
        std::cin >> temp->value;
        temp->Leftchild = NULL;
        temp->Rightchild = NULL;
        Store[i] = temp;
}
    int flag = 0;
    TreeNode* p = NULL;
    while (!flag) {
        int Min1 = 0, Min2 = 0;
        int count = 0;
        for (int i = 0; i < n; i++) {      
            if (Store[i] != NULL) {
                if ((Store[Min1]->value) > Store[i]->value)
                    Min1 = i;
            }
            else continue;
        }
        if (Min1 == 0) Min2 = 1;
        for (int i = 0; i < n; i++) {
            if (i == Min1) continue;
            if (Store[Min2] == NULL && Min2 < n) Min2++;
            if (Store[i] != NULL && Store[Min2] != NULL) {
                if (Store[i]->value < Store[Min2]->value)
                    Min2 = i;
            }
            else continue;
        }
        temp = new TreeNode;
        temp->value = Store[Min1]->value + Store[Min2]->value;
        temp->Leftchild = Store[Min1];
        temp->Rightchild = Store[Min2];
        Store[Min1] = temp;
        Store[Min2] = NULL;
        for (int i = 0; i < n; i++) {
            if (Store[i] != NULL) {
                p = Store[i];
                count++;
            }
            else continue;
        }
        if (count == 1) {
            return p;
        }
    }

}

void Show(TreeNode* tree) {       
    if (tree == NULL) return;
    cout << tree->value;
    if (tree->Leftchild) {
        cout << "(";
        Show(tree->Leftchild);
        cout << ",";
    }
    if (tree->Rightchild) {
        Show(tree->Rightchild);
        cout << ")";
    }
}
//求WPL
int WPL(TreeNode* tree, int length)
{
    TreeNode* p = tree;
    if (p==NULL)        //判断是不是空树
     return 0;
    else        //如果不是空树
    {
        if (!p->Leftchild && !p->Rightchild)
            return p->value * length;
        else
            return WPL(p->Leftchild, length + 1) + WPL(p->Rightchild, length + 1);
    }
}

void Huffmancode(TreeNode* huff, int length)
{
    TreeNode* p = huff;
    static int code[30];
    int i;
    if (p)
    {
        if (!p->Leftchild && !p->Rightchild)  //左右都是空的，找到叶子结点了！！！
        {
            for (i = 0; i < length; i++)
                cout << code[i];
            cout << " ";

        }
        else
        {
            code[length] = 0;
            Huffmancode(p->Leftchild, length + 1);
            code[length] = 1;
            Huffmancode(p->Rightchild, length + 1);
        }
    }
}


int main() {
    int n;
    std::cin >> n;
    TreeNode* tree;
    tree = creat(n);
    int wpl;
    wpl = 0;
    wpl = WPL(tree, wpl);
    cout << wpl << endl;
    Show(tree);
    cout << "\n";
    int length = 0;
    Huffmancode(tree, length);
}